<?php

namespace App\Http\Controllers\OtherSystem;
use App\HostDetail;
use App\HostType;
use Log;
use EasyWeChat\Foundation\Application;
use App\Http\Controllers\Controller;

class WechatController extends Controller
{
    public function serve()
    {
        Log::info('request arrived.'); # 注意：Log 为 Laravel 组件，所以它记的日志去 Laravel 日志看，而不是 EasyWeChat 日志

        $wechat = app('wechat');

        $wechat->server->setMessageHandler(function($message){
            switch ($message->MsgType) {
                case 'event':
                    return '收到事件消息';
                    break;
                case 'text':
                    $contents = explode(PHP_EOL,$message->Content);
                    if ($contents[0] == 'newhost'){
                        $newHostType = HostType::create([
                            'title' => $contents[1]
                        ]);
                        array_splice($contents,0,2);
                        foreach ($contents as $content){
                            HostDetail::create([
                               'host_type_id' =>  $newHostType->id,
                                'record' => $content
                            ]);
                        }
                        return '添加host成功';
                    }elseif($contents[0] == 'host'){
                        $hosttypes = HostType::all();
                        $arrays = json_decode($hosttypes, true);
                        $string = '';
                        foreach ($arrays as $array){
                            $string .= $array['id'].' '.$array['title'].PHP_EOL;
                        }
                        return $string;
                    }elseif(is_numeric($contents[0])){
                        $hosts = HostType::find($contents[0])->hosts;
                        $arrays = json_decode($hosts, true);
                        $string = '';
                        foreach ($arrays as $array){
                            $string .= $array['record'].PHP_EOL;
                        }
                        return $string;
                    }else{
                        return '收到文字消息';
                    }
                    break;
                case 'image':
                    return '收到图片消息';
                    break;
                case 'voice':
                    return '收到语音消息';
                    break;
                case 'video':
                    return '收到视频消息';
                    break;
                case 'location':
                    return '收到坐标消息';
                    break;
                case 'link':
                    return '收到链接消息';
                    break;
                // ... 其它消息
                default:
                    return '收到其它消息';
                    break;
            }
        });

        Log::info('return response.');

        return $wechat->server->serve();
    }

    public function user(Application $app){
        $userService = $app->user;
        return $userService->lists();
    }
}
